local dangyi = fk.CreateSkill{
  name = "pegasus__dangyi",
}

local DiscussionPindian = require "packages/pegasus/pkg/pegasus/GameEvent/discussion_pindian"

local prompts = {}
prompts.put = "#pegasus__dangyi-put"

Fk:loadTranslationTable{
  ["pegasus__dangyi"] = "荡异",
  [":pegasus__dangyi"] = "出牌阶段限一次，你可与所有其他角色进行视为议事的逐鹿，赢者（没有则为你）将任意张拼点牌置于牌堆顶，" ..
    "若意见为红/黑，你视为对意见与你相同/不同的角色使用一张【五谷丰登】/【兵临城下】。",
  ["#pegasus__dangyi"] = "荡异：可以与所有其他角色进行视为议事的逐鹿",

  [prompts.put] = "荡异：将任意张拼点牌置于牌堆顶",
}

dangyi:addEffect("active", {
  anim_type = "control",
  can_use = function (self, player)
    return player:usedSkillTimes(dangyi.name, Player.HistoryPhase) == 0 and
      table.find(Fk:currentRoom().alive_players, function (p)
        return player:canPindian(p)
      end)
  end,
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    local targets = table.filter(room.alive_players, function (p)
      return p == player or player:canPindian(p)
    end)

    local pindian = DiscussionPindian.discussion(player, targets, dangyi.name)
    local card_map = {
      [player] = pindian.fromCard,
    }
    for _, p in pairs(targets) do
      if p ~= player then
        card_map[p] = pindian.results[p].toCard
      end
    end

    local winner
    for i = 1, #targets do
      local is_winner = true
      for j = i, #targets do
        if i ~= j and card_map[targets[i]].number <= card_map[targets[j]].number then
          is_winner = false
        end
      end
      if is_winner then
        winner = targets[i]
        break
      end
    end
    if not winner then winner = player end

    local cards = table.map(targets, function (p)
      return p == player and room:getSubcardsByRule(pindian.fromCard)[1] or
        room:getSubcardsByRule(pindian.results[p].toCard)[1]
    end)
    cards = room:askToChooseCards(winner, {
      min = 0,
      max = #cards,
      target = player,
      flag = { card_data = { { "AskForDiscussion", cards } } },
      skill_name = dangyi.name,
      prompt = prompts.put,
    })

    if #cards > 0 then
      room:moveCards{
        ids = cards,
        toArea = Card.DrawPile,
        drawPilePosition = 1,
        proposer = winner,
        skillName = dangyi.name,
        moveReason = fk.ReasonPut,
        moveVisible = true,
      }
    end
    if player.dead then return end

    local card, tos
    if pindian.color == "red" then
      card = Fk:cloneCard("amazing_grace")
      card.skillName = dangyi.name
      tos = table.filter(targets, function (p)
        return pindian.results[p].opinion == pindian.results[player].opinion
          and player:canUseTo(card, p)
      end)
    elseif pindian.color == "black" then
      card = Fk:cloneCard("enemy_at_the_gates")
      card.skillName = dangyi.name
      tos = table.filter(targets, function (p)
        return pindian.results[p].opinion ~= pindian.results[player].opinion
          and player:canUseTo(card, p)
      end)
    end

    if card and tos and #tos > 0 then
      room:useCard{
        from = player,
        tos = tos,
        card = card,
      }
    end
  end
})

return dangyi